<template>
  <el-form ref="registerForm" :model="registerForm" :rules="rules" @submit.native.prevent="handleRegister">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="registerForm.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="registerForm.password"></el-input>
    </el-form-item>
    <el-button type="primary" native-type="submit">注册</el-button>
  </el-form>
</template>

<script>
export default {
  name: 'UserRegister',
  data() {
    return {
      registerForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          localStorage.setItem('username', this.registerForm.username);
          localStorage.setItem('password', this.registerForm.password);
          this.$message.success('注册成功，请登录');
        } else {
          this.$message.error('表单验证失败');
        }
      });
    }
  }
};
</script>